package com.qiaomeng.flutter.flutter_album_save;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
 * FlutterAlbumSavePlugin
 */
public class FlutterAlbumSavePlugin implements FlutterPlugin, MethodCallHandler {

    private MethodChannel channel;
    private SaveHandler handler;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_album_save");
        channel.setMethodCallHandler(this);
        handler = new SaveHandler(flutterPluginBinding.getApplicationContext());
    }

    public static void registerWith(Registrar registrar) {
        final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_album_save");
        channel.setMethodCallHandler(new FlutterAlbumSavePlugin());
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.method.equals("saveImageToAlbum")) {
            String imagePath = call.argument("imagePath");
            handler.saveImageToAlbum(imagePath, result);
            ;
        } else {
            result.notImplemented();
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }
}
